//标记被选中节点
	var resverseTypes=[6,61];
	var resverseTypesVal=[6,61];
	var selectNode=null;
	var isNeedTake = false;
	var alreadyTake = false;
	var checkStr = "";
	var changeStr = "";
	var treeNodes=[];
	var setting = {
		data: {
			simpleData: {
				enable: true
			}
		},
		callback:{
			onClick:zTreeOnClick,
			beforeClick:zTreebeforeClick
		}
	};
	
	$(document).ready(function(){
		getTreeData();
		$("#resverseTypes").val(resverseTypesVal.join());
		$.fn.zTree.init($("#sptree"), setting, treeNodes);
		getDefaultSelectIds();
		select1("stockQueryForm");
		getCheckStr();
	});
	
	/*获得商品分类的信息*/
	function getTreeData()
	{
		$.ajax({
			url:basePath + "scmInvocing/productInfo/findCategoryList.shtml",
			data:{"resverseTypes":resverseTypes.join()},
			dataType:"json",
			type:"post",
	 		async:false,
			success:function(result)
			{
				var node;
				$(result).each(function(id,item)
					{
					if(id==0)
					{
    	 				node={id:item.pkPrdcateId,pId:item.fkParPrdcateId,name:item.cateName,open:true,reserveFlag:item.reserveFlag,resverseType:item.resverseType};
   	 				}else
   	 				{
    	 				node={id:item.pkPrdcateId,pId:item.fkParPrdcateId,name:item.cateName,reserveFlag:item.reserveFlag,resverseType:item.resverseType};
   	 				}
						treeNodes.push(node);
					});
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});
	}

	
	function zTreebeforeClick()
	{
		//检测是否需要盘点
		if(!getChangeStr())
			{
				alertError("检测到有需要提交的盘点项，请点击确定提交！");
				return false;
			}
	}
	
	function zTreeOnClick(event, treeId, treeNode) {
		selectNode = treeNode;// 每次点击赋值 
		//点击根节点
		if (selectNode.getParentNode() == null) {
				var selectId = [];
				selectId = getSelectIds(selectId, treeNode);
				$("#selectId").val(selectId.join());
				select1("stockQueryForm");
		} else {
			//选中的商品id
			var selectId = [];
			selectId = getSelectIds(selectId, treeNode);
			$("#selectId").val(selectId.join());
			select1("stockQueryForm");
		}
		getCheckStr();
	};

	//默认选中普通商品并显示普通商品以及子类商品
	function getDefaultSelectIds()
	{
		var treeObj = $.fn.zTree.getZTreeObj("sptree");
		var node = treeObj.getNodeByParam("name", "普通商品", null);
		treeObj.selectNode(node);
		var selectId=[];
		selectId = getSelectIds(selectId, node);
		$("#selectId").val(selectId.join());
	}
	
	//嵌套循环获得被选中商品及其子类商品的id
	function getSelectIds(selectId, treeNode) {
		selectId.push(treeNode.id);
		if (treeNode.isParent) {
			for ( var obj in treeNode.children) {
				getSelectIds(selectId, treeNode.children[obj]);
			}
		}
		return selectId;
	}

	//库存盘点显示
	function initTable(formId)
	{
		$.ajax({
			url:basePath + "StockQuery/findProductListForTake.shtml",
			data:$("#"+formId).serialize(),
			dataType:"json",
			type:"post",
			async:false,
			success:function(result)
			{
				callback(result);
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});
	}
	
	function callback(data) {
		if (data == null) {
			return;
		}
		trimJsonArray(data);//去除null
		var htm="";
		document.getElementById("addStockTakeTable").style.display = "";
		$.each(data,function(ind, obj) {
				htm += "<tr>";
				var num = (obj.stgCnt == "" ? 0 : obj.stgCnt);
				htm += "<td>" + (ind+1) + "</td>";
				htm += "<td>" + obj.pkPrdId + "</td>";
				htm += "<td>" + obj.prdName + "</td>";
				htm += "<td>" + obj.cateName + "</td>";
				htm += "<td>" + obj.prdSpec + "</td>";
				htm += "<td>" + obj.unitName + "</td>";
				htm += "<td>" + num + "</td>";
				if(typeof(obj.stkCnt) == 'number')
					{
						htm += "<td><input type='text'  class='iput_t w100' value ='"+ obj.stkCnt+ "' onchange='diffCut(this,"+ num + ");'></td>";
						htm += "<td>"+(num - obj.stkCnt)+"</td>";
						htm += "<td><input type='text' class='iput_t w100' value='"+obj.comments+"'></td></tr>";
					}
				else
					{
						htm += "<td><input type='text' class='iput_t w100' onchange='diffCut(this,"+ num + ");'></td>";
						htm += "<td></td>";
						htm += "<td><input type='text' class='iput_t w100'></td></tr>";
					}
		});
		$("#tbody").html(htm);
	}

	//填写实际数量后，计算与库存的差异数
	function diffCut(obj, stgCnt) {
		var val = $.trim($(obj).val());
		if (val != "")//去空格
		{
			if (isNaN(val) || (val.indexOf(".") > -1)) {
				alertError("请输入整数");
				$(obj).val("");
				$(obj)[0].focus();
				$(obj).parent().parent().find("td:eq(8)").html("");
				return;
			}
		} else {
			$(obj).parent().parent().find("td:eq(8)").html("");
			return;
		}
		if (val < 0 || (val.indexOf("-") > -1)) {
			alertError("请输入正数");
			$(obj).val("");
			return;
		}

		var result = stgCnt - val;
		alreadyTake = false;
		$(obj).parent().parent().find("td:eq(8)").html(result);
	}

	//设置pageNo为1
	function select1(formId) {
		$("#" + formId + " input:hidden[name='pageNo']").val(1);
		initTable(formId);
	}

	//列表初始的盘点数量组成数列：“12332”(如果盘点数量为“”用n代替)
	function getCheckStr()
	{
		var s = "";
		$("#addStockTakeTable tr").each(function() {
			var stkCnt = $(this).find("td:eq(7)").find("input").val();
			if(stkCnt == "")
				{
					stkCnt = "n";
				}
			s += stkCnt;
		});
		checkStr = s;
	}	
	
	//列表改变后的盘点数量组成数列：“12332”(如果盘点数量为“”用n代替)
	function getChangeStr()
	{
		var s = "";
		$("#addStockTakeTable tr").each(function() {
			var stkCnt = $(this).find("td:eq(7)").find("input").val();
			if(stkCnt == "")
			{
				stkCnt = "n";
			}
			s += stkCnt;
		});
		changeStr = s;
		if(changeStr == checkStr)
			{
				return true;
			}
		else
			{
				return false;
			}
	}
	//提交盘点
	function doTakeStock() {
		var takestkContent = "";
		$("#addStockTakeTable tr").each(
			function() {
				var prdId = $(this).find("td:eq(1)").html();
				var stgCnt = $(this).find("td:eq(6)").html();
				var stkCnt = $(this).find("td:eq(7)").find("input").val();
				var comments = $(this).find("td:eq(9)").find(
						"input").val();
				if ($.trim(stkCnt) != "" || $.trim(comments) != "") {
					takestkContent += prdId + "_" + stgCnt + "_"
							+ stkCnt + "_" + comments + "|";
				}

			});
		$("#takestkContent").val(takestkContent);
		$.ajax({
			url : basePath + "StockTaking/doTakeStock.shtml",
			data : $("#doTakeStockFrom").serialize(),
			dataType : "text",
			type : "post",
			async : false,
			success : function(result) {
				if (result == 1) {
					alreadyTake = true;//表示已经成功提交盘点
					alertInfo("盘点提交成功");
					getCheckStr();
					updateCount();
				} else {
					alreadyTake = false;
					alertError("盘点提交失败，请检查网络");
				}
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});
	}
	
	//更新盘点的商品项数
	function updateCount()
	{
		$.ajax({
			url : basePath + "StockTaking/findTakestkPrdCount.shtml",
			data : {"fkTskId" : fkTskId},
			dataType : "text",
			type : "post",
			async : false,
			success : function(result) {
				$("#prdCnt").html(result);
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});
	}
	
	//离开页面前检测是否需要提骄傲盘点
	/* 	window.onbeforeunload = function(){
		
		//检测是否需要盘点
			checkTakeStock();
		if (isNeedTake && !alreadyTake) 
			{
				$.messager.confirm('My Title', 'Are you confirm this?', function(r) {
				if (r) {
					alert('confirmed: ' + r);
				}
				else
				{
					return;
				}
			});
		}
	} */